﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;
using DefaultScope;
using Fabrikam.Phone.DataModel;
using Microsoft.Phone.Controls;

namespace Fabrikam.Phone.ViewModels
{
    public class ServiceTicketViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public ServiceTickets ServiceTicket { get; set; }
        public RelayCommand SaveCommand { get; set; }
        public RelayCommand DeleteCommand { get; set; }
        public bool IsDirty { get; set; }
        public bool IsNew { get; set; }

        private List<Customers> customers;
        public List<Customers> Customers
        {
            get { 
                return customers; }
            set {
                if (value == customers)
                    return;

                customers = value;

                this.RaisePropertyChanged("Customers");

            }
        }

        public ServiceTicketViewModel(ServiceTickets ticket, Customers customer, List<Customers> customers)
            : this()
        {
            this.ServiceTicket = ticket;
            this.Customers = customers;
            this.Customer = customer;
            this.IsNew = false;
        }

        public ServiceTicketViewModel()
        {
            this.AvailableStates.Add(1);
            this.AvailableStates.Add(2);
            this.AvailableStates.Add(3);

            this.IsNew = true;
      
            this.PropertyChanged += (sender, args) => this.IsDirty = true;
            this.SaveCommand = new RelayCommand(async () => await Save());
            this.DeleteCommand = new RelayCommand(Delete);
        }

      

        private async Task Save()
        {

           // await DataService.Current.InsertServiceTickets(this);

            CustomMessageBox messageBox = new CustomMessageBox
                {
                Caption = "Fabrikam Fiber",
                Message = "Ticket not saved becoz not implemented yet !",
                LeftButtonContent = "ok",
                IsFullScreen = false
            };

            messageBox.Unloaded += (sender, args) =>
            {

                PhoneApplicationPage page = App.RootFrame.Content as PhoneApplicationPage;
                if (page != null)
                    page.Dispatcher.BeginInvoke(() =>
                                                page.NavigationService.Navigate(new Uri("/MainPage.xaml",
                                                                                        UriKind.Relative)));
            };
            messageBox.Show();

        }

        private void Delete()
        {

            CustomMessageBox messageBox = new CustomMessageBox
                {
                Caption = "Fabrikam Fiber",
                Message = "Are you sure you want delete this item ?",
                LeftButtonContent = "ok",
                RightButtonContent = "cancel",
                IsFullScreen = false
            };
            Boolean mustGoToMainPage = false;
            messageBox.Dismissed += async (s1, e1) =>
            {

                switch (e1.Result)
                {
                    case CustomMessageBoxResult.LeftButton:
                        await DataService.Current.DeleteTicket(this.ServiceTicket.ServiceTicketID);

                        mustGoToMainPage = true;
                        break;
                    case CustomMessageBoxResult.RightButton:
                        break;
                }

            };
            messageBox.Unloaded += (sender, args) =>
                {
                    if (!mustGoToMainPage)
                        return;

                    PhoneApplicationPage page = App.RootFrame.Content as PhoneApplicationPage;
                    if (page != null)
                        page.Dispatcher.BeginInvoke(() =>
                                                    page.NavigationService.Navigate(new Uri("/MainPage.xaml",
                                                                                            UriKind.Relative)));
                };

            messageBox.Show();
        }
      
        /// <summary>
        /// The <see cref="AvailableStates" /> property's name.
        /// </summary>
        public const string AvailableStatesPropertyName = "AvailableStates";

        private ObservableCollection<Int32> availableStates = new ObservableCollection<Int32>();

        /// <summary>
        /// Sets and gets the AvailableStates property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<Int32> AvailableStates
        {
            get
            {
                return availableStates;
            }

            set
            {
                if (availableStates == value)
                {
                    return;
                }

                availableStates = value;
                RaisePropertyChanged(AvailableStatesPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="Customer" /> property's name.
        /// </summary>
        public const string CustomerPropertyName = "Customer";

        private Customers customer;

        /// <summary>
        /// Sets and gets the Customer property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Customers Customer
        {
            get
            {
                return customer;
            }

            set
            {
                if (customer == value)
                {
                    return;
                }

                customer = value;

                this.ServiceTicket.CustomerID = customer == null ? (int?) null : customer.ID;

                RaisePropertyChanged(CustomerPropertyName);
                RaisePropertyChanged(Address_StatePropertyName);
                RaisePropertyChanged(Address_ZipPropertyName);
                RaisePropertyChanged(Address_CityPropertyName);
                RaisePropertyChanged(Address_StreetPropertyName);
            }
        }

        public const string ClosedPropertyName = "Closed";

        public String ClosedString
        {
            get
            {
                if (this.ServiceTicket.Closed.HasValue)
                    return this.ServiceTicket.Closed.Value.Date.ToString("ddd, MMM d, yyyy");

                return null;
            }
        }
        public String OpenedString
        {
            get
            {
                if (this.ServiceTicket.Opened.HasValue)
                    return this.ServiceTicket.Opened.Value.Date.ToString("ddd, MMM d, yyyy");

                return null;
            }
        }

        public DateTime? Closed
        {
            get
            {
                if (this.ServiceTicket.Closed.HasValue)
                    return this.ServiceTicket.Closed.Value.Date;

                return null;
            }

            set
            {
                if (value == null && !this.ServiceTicket.Closed.HasValue)
                    return;

                if (this.ServiceTicket.Closed.HasValue && this.ServiceTicket.Closed.Value.Date == value)
                    return;

                if (value == null)
                    this.ServiceTicket.Closed = null;
                else
                    this.ServiceTicket.Closed = value;

                RaisePropertyChanged(ClosedPropertyName);
            }
        }

        public const string OpenedPropertyName = "Opened";

        public DateTime? Opened
        {
            get
            {
                if (this.ServiceTicket.Opened.HasValue)
                    return this.ServiceTicket.Opened.Value.Date;

                return null;
            }

            set
            {
                if (value == null && !this.ServiceTicket.Opened.HasValue)
                    return;

                if (this.ServiceTicket.Opened.HasValue && this.ServiceTicket.Opened.Value.Date == value)
                    return;

                if (value == null)
                    this.ServiceTicket.Opened = null;
                else
                    this.ServiceTicket.Opened = value;

                RaisePropertyChanged(OpenedPropertyName);
            }
        }

        public const String EscalationLevelPropertyName = "EscalationLevel";

        public int EscalationLevel
        {
            get
            {
                return this.ServiceTicket.EscalationLevel;
            }

            set
            {
                if (this.ServiceTicket.EscalationLevel == value)
                {
                    return;
                }

                this.ServiceTicket.EscalationLevel = value;
                RaisePropertyChanged(EscalationLevelPropertyName);
            }
        }

        public const string StatusValuePropertyName = "StatusValue";

        public int StatusValue
        {
            get
            {
                return this.ServiceTicket.StatusValue;
            }

            set
            {
                if (this.ServiceTicket.StatusValue == value)
                {
                    return;
                }

                this.ServiceTicket.StatusValue = value;

                switch (value)
                {
                    case 1: this.Closed = DateTime.Now;
                        break;
                    case 2:
                        this.Closed = DateTime.Now;
                        break;
                    default:
                        this.Closed = null;
                        break;
                }

                RaisePropertyChanged(StatusValuePropertyName);
            }
        }

        public string CustomerFullName
        {
            get { return this.Customer.FirstName + " " + this.Customer.LastName; }
        }

  
        public const String Address_StreetPropertyName = "Address_Street";
        public string Address_Street
        {
            get
            {
                return Customer.Address_Street;
            }
            set
            {
                Customer.Address_Street = value;
                RaisePropertyChanged(Address_StreetPropertyName);
            }
        }

        public const String Address_CityPropertyName = "Address_City";
        public string Address_City
        {
            get
            {
                return Customer.Address_City;
            }
            set
            {
                Customer.Address_City = value;
                RaisePropertyChanged(Address_CityPropertyName);
            }
        }

        public const String Address_StatePropertyName = "Address_State";
        public string Address_State
        {
            get
            {
                return Customer.Address_State;
            }
            set
            {
                Customer.Address_State = value;
                RaisePropertyChanged(Address_StatePropertyName);
            }
        }

        public const String Address_ZipPropertyName = "Address_Zip";
        public string Address_Zip
        {
            get
            {
                return Customer.Address_Zip;
            }
            set
            {
                Customer.Address_Zip = value;
                RaisePropertyChanged(Address_ZipPropertyName);
            }
        }

        public string CustomerFullAddress
        {
            get
            {
                return this.Customer.Address_Street + Environment.NewLine +
                         this.Customer.Address_Zip + " " + this.Customer.Address_City +
                         this.Customer.Address_State;
            }
        }



    }
}
